package com.hyp.pay.UI.Home.presenter;

import android.util.Log;

import com.hyp.pay.UI.Home.presenter.contract.IdCardContract;
import com.hyp.pay.base.RxPresenter;
import com.hyp.pay.http.Constants;
import com.hyp.pay.http.RetrofitHelper;
import com.hyp.pay.model.UpLodAttion;
import com.hyp.pay.model.UplodIdcard;

import java.io.File;

import javax.inject.Inject;

import io.reactivex.Flowable;
import io.reactivex.subscribers.ResourceSubscriber;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2017/6/2.
 */

public class IdCardPresenter extends RxPresenter<IdCardContract.View> implements IdCardContract.Presenter{

    private RetrofitHelper mRetrofitHelper;

    @Inject
    public IdCardPresenter(RetrofitHelper mRetrofitHelper) {
        this.mRetrofitHelper = mRetrofitHelper;
    }




    @Override
    public void getUploadFileIDCard(String filePath, int userid) {
        File file = new File(filePath);

        RequestBody requestFile = RequestBody.create(MediaType.parse("image/*"), file);

        MultipartBody.Part body =
                MultipartBody.Part.createFormData("positiveFile", file.getName(), requestFile);

        Flowable<UplodIdcard> flowable = mRetrofitHelper.UploadFileIdCard(body,userid);
        ResourceSubscriber subscriber = mRetrofitHelper.startObservable(flowable, new ResourceSubscriber<UplodIdcard>() {

            @Override
            public void onNext(UplodIdcard data) {

                Log.i("olj",data.toString());
                if (getView() != null) {
                    getView().showUplod(data);
                }

            }

            @Override
            public void onError(Throwable t) {
                t.printStackTrace();
                if (getView() != null)
                    getView().showError(Constants.GRS_ERROR);
            }

            @Override
            public void onComplete() {

            }
        });

        addSubscription(subscriber);
    }

    @Override
    public void getUploadFileIDCard2(String filePath, int userid) {
        File file = new File(filePath);

        RequestBody requestFile = RequestBody.create(MediaType.parse("image/*"), file);


        MultipartBody.Part body =
                MultipartBody.Part.createFormData("negativeFile", file.getName(), requestFile);


        Flowable<UplodIdcard> flowable = mRetrofitHelper.UploadFileIdCard(body,userid);
        ResourceSubscriber subscriber = mRetrofitHelper.startObservable(flowable, new ResourceSubscriber<UplodIdcard>() {

            @Override
            public void onNext(UplodIdcard data) {

                Log.i("olj",data.toString());
                if (getView() != null) {
                    getView().showUplod2(data);
                }

            }

            @Override
            public void onError(Throwable t) {
                t.printStackTrace();
                if (getView() != null)
                    getView().showError(Constants.GRS_ERROR);
            }

            @Override
            public void onComplete() {

            }
        });

        addSubscription(subscriber);
    }

    @Override
    public void getUploadIDCard(String filePath, String filePath2, int userid) {
//        File file = new File(filePath);
//        File file2 = new File(filePath2);
//        RequestBody requestFile = RequestBody.create(MediaType.parse("image/*"), file);
//
//        RequestBody requestFile2= RequestBody.create(MediaType.parse("image/*"), file2);
//
//        MultipartBody.Part body =
//                MultipartBody.Part.createFormData("positiveFile", file.getName(), requestFile);
//
//        MultipartBody.Part body2 =
//                MultipartBody.Part.createFormData("negativeFile", file.getName(), requestFile2);


        Flowable<UpLodAttion> flowable = mRetrofitHelper.UploadIdCard(filePath,filePath2,userid);
        ResourceSubscriber subscriber = mRetrofitHelper.startObservable(flowable, new ResourceSubscriber<UpLodAttion>() {

            @Override
            public void onNext(UpLodAttion data) {

                Log.i("olj",data.toString());
                if (getView() != null) {
                    getView().showUplodAttion(data);
                }

            }

            @Override
            public void onError(Throwable t) {
                t.printStackTrace();
                if (getView() != null)
                    getView().showError(Constants.GRS_ERROR);
            }

            @Override
            public void onComplete() {

            }
        });

        addSubscription(subscriber);
    }
}
